package com.fi.common.command;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;

import com.fi.common.file.FileTool;
import com.fi.common.log.FiLog;
import com.google.gson.reflect.TypeToken;

import fivm.cross.platform.ficommon.HttpStandardMsg.HTTPPayloadResp;
import fivm.cross.platform.steamobj.controller.CommandStream;
import fivm.cross.platform.steamobj.sys.DeviceControlStream;
import fivm.cross.platform.steamobj.sys.DeviceConfigs;

/**
 * 系统级的命令行控制
 * */
public class CommandClientControl {
	private final static String TAG="CommandClientControl";
	private final String CmdUrlPath="/getCmds";
	private String cmdUrl="";
	private DeviceConfigs startupConfigs;
	private String actionName;
	private CommandStream commandStream;
	private CmdRunner cmdRunner;		//命令的处理直接放这里算了
	
	
	public CommandClientControl(DeviceConfigs config){
		startupConfigs=config;
		cmdUrl=startupConfigs.genUrl(CmdUrlPath);
	}
	public CommandClientControl(DeviceConfigs config,String actionName){
		startupConfigs=config;
		cmdUrl=startupConfigs.genUrl(CmdUrlPath);
	}
	
	/**
	 * 将shell执行起来，干脆总体封装在这里
	 * */
	public CmdRet execShell() throws UnsupportedEncodingException {
		if(!isCmdValied()){
			return null;
		}
		return execShell(commandStream);
	}
	
	/**
	 * 将shell执行起来，将要运行的指令名称直接放这里算了
	 * */
	public CmdRet execShell(String actionName) throws UnsupportedEncodingException {
		CommandStream commandStream=getCommandStreamByActionName(actionName);
		return execShell(commandStream);
	}
	/**
	 * 最终回到这里
	 * */
	public static CmdRet execShell(CommandStream commandStream){
		String shellCmd=null;
		if(commandStream.getCarrier()==CommandStream.SHELL_CARRIER_FILE){
			/**
			 * 我们要判断一下文件存在
			 * */
			String cmdMsg=commandStream.getCmds();
			if(cmdMsg==null || cmdMsg.equals("")){
				FiLog.appLogE(TAG,"execShell ..CARRIER  is file , but cmdMsg is empty ");
				return null;
			}
           String clientFilePath=commandStream.getClientFilePath();
           streamToShellFile(clientFilePath,cmdMsg);
			//如果执行体是以shell文件，那我们就应该运行shell 命令调用.sh文件
			shellCmd=commandStream.getShell();
		} else{
			shellCmd=commandStream.getCmds();
		}
		return new CmdRunner().execCmd(shellCmd);
	}
	
	
	public boolean isCmdValied() throws UnsupportedEncodingException {
		if(commandStream==null){//如果命令不存在，那么我们就到服务器上请求
			if(actionName==null || actionName.equals("")){
				FiLog.appLogE(TAG,"the shell name is null");
				return false;
			}
			commandStream=getCommandStreamByActionName(actionName);
			if(commandStream==null){
				FiLog.appLogE(TAG,"get CommandStream failed : actionName is " + actionName);
				return false;
			}
		}
		return true;
	}
	

	
	
	
	
	public CommandStream getCommandStreamByActionName(String actionName) throws UnsupportedEncodingException {
		HashMap<String ,String> params=new HashMap<String ,String>();
		params.put("actionName", actionName);
		CommandStream commandStream=CommandStream.toHttp(cmdUrl, params, CommandStream.class);
		//在这里我们对命令进行优化处理
		commandStream=optimizeCommandStream(commandStream);
		return commandStream;
	}
	
	
	 /**
     * 从数据库取出来，还是需要一定程度进行处理
     * */
    public CommandStream optimizeCommandStream(CommandStream commandStream){
    	if(commandStream==null){
    		FiLog.appLogV(TAG,"optimizeCommandStream function : the CommandStream is null");
    		return null;
    	}
    	/**
    	 * 如果是通过运行shell来完成操控的，那么我们就执行将msg写入到文件里面去
    	 * */
    	if(commandStream.getCarrier()==CommandStream.SHELL_CARRIER_FILE){
            String cmdContent=commandStream.getCmds();
            if(cmdContent==null){
                FiLog.appLogE(TAG,"get the cmds msg string is null ");
                return null;
            }

//            FileTool.writeFile(clientFilePath, cmdContent,false);		//将内空写进.sh文件里面去
//            //顺便要改变它的运行属性
//            CmdFrequently.chmod(clientFilePath,777);
        }
        return commandStream;
    }
    
    public static void streamToShellFile(String path,String cmdContent){
        FileTool.writeFile(path, cmdContent,false);		//将内空写进.sh文件里面去
        //顺便要改变它的运行属性
        CmdFrequently.chmod(path,777);
    }
   
    
    
	
	public String getActionName() {
		return actionName;
	}
	public void setActionName(String actionName) {
		this.actionName = actionName;
	}
	public CommandStream getCommandStream() {
		return commandStream;
	}
	public void setCommandStream(CommandStream commandStream) {
		this.commandStream = commandStream;
	}
	
	
}
